home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / arith2.com / ARITH_H.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-02-27  |  3.8 KB  |  118 lines

  1. UNIT arith_h;
  2.  
  3.         { ------------------------------------------------------------------
  4.  
  5.           This program and its associates implement in Turbo Pascal v5
  6.           the aritmetic encoding/decoding algorithms presented in the papers
  7.  
  8.           "Arithmetic Coding for Data Compression"
  9.  
  10.                    by Ian     H. Witten
  11.                       Radford M. Neal
  12.                       John    G. Cleary
  13.  
  14.           pp 520 - 540 of June 1987 Communications of the ACM
  15.  
  16.           and
  17.  
  18.           "An Adaptive Dependency Source Model For Data Compression"
  19.  
  20.                    by David M. Abrahamson
  21.  
  22.           pp 77 - 83 of January 1989 Communications of the ACM
  23.  
  24.           ------------------------------------------------------------------
  25.  
  26.           Implemented by Ken Westerback : CompuServe 73547,3520
  27.  
  28.           version 1.0 released 89/02/19
  29.           version 2.0 released 89/02/27
  30.  
  31.           These programs, units and associated documentation are released
  32.           into the public domain to be used and abused as your whims
  33.           dictate.
  34.  
  35.           Feel free to distribute/incorporate/improve as desired.
  36.  
  37.           >>>>> Use at your own risk! <<<<<
  38.  
  39.           Comments and suggestions welcome via CompuServe.
  40.  
  41.           ------------------------------------------------------------------
  42.         }
  43.  
  44.  
  45. INTERFACE uses dos;
  46.  
  47. type code_value = word; { type of an arithmetic code value }
  48.      bit_buffer = array [ 0..511 ] of longint; { 1 allocation unit of 2048 bytes }
  49.  
  50.  
  51. const code_value_bits = SizeOf ( code_value ) * 8;   { number of bits in a code value }
  52.  
  53.       top_value       = (1 shl code_value_bits) - 1; { largest code value             }
  54.  
  55.       first_qtr       = (top_value div 4) + 1;       { point after first quarter      }
  56.       half            = 2 * first_qtr;               { point after first half         }
  57.       third_qtr       = 3 * first_qtr;               { point after third quarter      }
  58.  
  59.       bits_per_buffer = 32;
  60.  
  61.       high_bit = 1 shl ( bits_per_buffer - 1 );
  62.  
  63.       one_masks : array [ 1..32 ] of longint =
  64.                      ( $80000000, $C0000000, $E0000000, $F0000000
  65.                       ,$F8000000, $FC000000, $FE000000, $FF000000
  66.                       ,$FF800000, $FFC00000, $FFE00000, $FFF00000
  67.                       ,$FFF80000, $FFFC0000, $FFFE0000, $FFFF0000
  68.                       ,$FFFF8000, $FFFFC000, $FFFFE000, $FFFFF000
  69.                       ,$FFFFF800, $FFFFFC00, $FFFFFE00, $FFFFFF00
  70.                       ,$FFFFFF80, $FFFFFFC0, $FFFFFFE0, $FFFFFFF0
  71.                       ,$FFFFFFF8, $FFFFFFFC, $FFFFFFFE, $FFFFFFFF
  72.                      );
  73.  
  74. var bits_sent    : longint; { counts of bits/chars for use in displays }
  75.     bits_gotten  : longint; {    and calculations                      }
  76.  
  77.     buffer     : longint;    { where the bits are written to or read from }
  78.     big_buffer : bit_buffer;
  79.     bits_file  : file;
  80.  
  81.     buffer_index : longint;   { index into bit_buffer of word in buffer    }
  82.  
  83.     bits_to_go : 0..32;       { number of bits still waiting in buffer     }
  84.  
  85.     sending_crap : boolean;   { true if input exhausted & random bits sent }
  86.  
  87.     bits_to_follow : longint; { # of opposite bits to send after next bit  }
  88.  
  89.     value : code_value; { currently seen code value       }
  90.     low   : code_value; { low  end of current value range }
  91.     high  : code_value; { high  "   "    "      "     "   }
  92.  
  93.  
  94. IMPLEMENTATION
  95.  
  96. BEGIN
  97.  
  98. { initialize our variables to safe values, cuz others are untrustworthy! }
  99.  
  100. bits_sent      := 0;
  101. bits_gotten    := 0;
  102. buffer         := 0;
  103. buffer_index   := 0;
  104. bits_to_follow := 0;
  105. bits_to_go     := 0;
  106.  
  107. { start with largest possible range and no value }
  108.  
  109. value := 0;
  110.  
  111. low   := 0;
  112. high  := top_value;
  113.  
  114. sending_crap := false; { assume the best }
  115.  
  116. fillchar ( big_buffer, sizeof(big_buffer), 0 );
  117.  
  118. END. { arithmetic coding header }